/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha3

import (
	"reflect"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
	"k8s.io/utils/ptr"
	"sigs.k8s.io/controller-runtime/pkg/conversion"

	addonsv1 "sigs.k8s.io/cluster-api/api/addons/v1beta2"
	clusterv1alpha3 "sigs.k8s.io/cluster-api/internal/api/core/v1alpha3"
	utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

func (src *ClusterResourceSet) ConvertTo(dstRaw conversion.Hub) error {
	dst := dstRaw.(*addonsv1.ClusterResourceSet)

	if err := Convert_v1alpha3_ClusterResourceSet_To_v1beta2_ClusterResourceSet(src, dst, nil); err != nil {
		return err
	}

	// Reset conditions from autogenerated conversions
	// NOTE: v1alpha3 conditions should not be automatically be converted into v1beta2 conditions.
	dst.Status.Conditions = nil

	// Move legacy conditions (v1alpha3) to the deprecated field.
	if src.Status.Conditions != nil {
		dst.Status.Deprecated = &addonsv1.ClusterResourceSetDeprecatedStatus{}
		dst.Status.Deprecated.V1Beta1 = &addonsv1.ClusterResourceSetV1Beta1DeprecatedStatus{}
		clusterv1alpha3.Convert_v1alpha3_Conditions_To_v1beta2_Deprecated_V1Beta1_Conditions(&src.Status.Conditions, &dst.Status.Deprecated.V1Beta1.Conditions)
	}

	// Manually restore data.
	restored := &addonsv1.ClusterResourceSet{}
	if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
		return err
	}
	dst.Status.Conditions = restored.Status.Conditions

	return nil
}

func (dst *ClusterResourceSet) ConvertFrom(srcRaw conversion.Hub) error {
	src := srcRaw.(*addonsv1.ClusterResourceSet)

	if err := Convert_v1beta2_ClusterResourceSet_To_v1alpha3_ClusterResourceSet(src, dst, nil); err != nil {
		return err
	}

	// Reset conditions from autogenerated conversions
	// NOTE: v1beta2 conditions should not be automatically be converted into legacy conditions (v1alpha3).
	dst.Status.Conditions = nil

	// Retrieve legacy conditions (v1alpha3) from the deprecated field.
	if src.Status.Deprecated != nil {
		if src.Status.Deprecated.V1Beta1 != nil {
			if src.Status.Deprecated.V1Beta1.Conditions != nil {
				clusterv1alpha3.Convert_v1beta2_Deprecated_V1Beta1_Conditions_To_v1alpha3_Conditions(&src.Status.Deprecated.V1Beta1.Conditions, &dst.Status.Conditions)
			}
		}
	}

	// Preserve Hub data on down-conversion except for metadata
	return utilconversion.MarshalData(src, dst)
}

func (src *ClusterResourceSetBinding) ConvertTo(dstRaw conversion.Hub) error {
	dst := dstRaw.(*addonsv1.ClusterResourceSetBinding)

	if err := Convert_v1alpha3_ClusterResourceSetBinding_To_v1beta2_ClusterResourceSetBinding(src, dst, nil); err != nil {
		return err
	}
	// Manually restore data.
	restored := &addonsv1.ClusterResourceSetBinding{}
	if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
		return err
	}
	dst.Spec.ClusterName = restored.Spec.ClusterName
	return nil
}

func (dst *ClusterResourceSetBinding) ConvertFrom(srcRaw conversion.Hub) error {
	src := srcRaw.(*addonsv1.ClusterResourceSetBinding)

	if err := Convert_v1beta2_ClusterResourceSetBinding_To_v1alpha3_ClusterResourceSetBinding(src, dst, nil); err != nil {
		return err
	}

	// Preserve Hub data on down-conversion except for metadata
	if err := utilconversion.MarshalData(src, dst); err != nil {
		return err
	}

	return nil
}

// Convert_v1beta2_ClusterResourceSetBindingSpec_To_v1alpha3_ClusterResourceSetBindingSpec is a conversion function.
func Convert_v1beta2_ClusterResourceSetBindingSpec_To_v1alpha3_ClusterResourceSetBindingSpec(in *addonsv1.ClusterResourceSetBindingSpec, out *ClusterResourceSetBindingSpec, s apimachineryconversion.Scope) error {
	// Spec.ClusterName does not exist in ClusterResourceSetBinding v1alpha3 API.
	return autoConvert_v1beta2_ClusterResourceSetBindingSpec_To_v1alpha3_ClusterResourceSetBindingSpec(in, out, s)
}

func Convert_v1beta2_ClusterResourceSetStatus_To_v1alpha3_ClusterResourceSetStatus(in *addonsv1.ClusterResourceSetStatus, out *ClusterResourceSetStatus, s apimachineryconversion.Scope) error {
	// V1Beta2 was added in v1beta1
	return autoConvert_v1beta2_ClusterResourceSetStatus_To_v1alpha3_ClusterResourceSetStatus(in, out, s)
}

func Convert_Pointer_v1alpha3_ResourceSetBinding_To_v1beta2_ResourceSetBinding(in **ResourceSetBinding, out *addonsv1.ResourceSetBinding, s apimachineryconversion.Scope) error {
	if in == nil || *in == nil {
		return nil
	}
	return autoConvert_v1alpha3_ResourceSetBinding_To_v1beta2_ResourceSetBinding(*in, out, s)
}

func Convert_v1beta2_ResourceSetBinding_To_Pointer_v1alpha3_ResourceSetBinding(in *addonsv1.ResourceSetBinding, out **ResourceSetBinding, s apimachineryconversion.Scope) error {
	if in == nil || reflect.DeepEqual(*in, addonsv1.ResourceSetBinding{}) {
		return nil
	}
	*out = &ResourceSetBinding{}
	return autoConvert_v1beta2_ResourceSetBinding_To_v1alpha3_ResourceSetBinding(in, *out, s)
}

func Convert_v1alpha3_ResourceBinding_To_v1beta2_ResourceBinding(in *ResourceBinding, out *addonsv1.ResourceBinding, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1alpha3_ResourceBinding_To_v1beta2_ResourceBinding(in, out, s); err != nil {
		return err
	}
	if in.LastAppliedTime != nil && !reflect.DeepEqual(in.LastAppliedTime, &metav1.Time{}) {
		out.LastAppliedTime = *in.LastAppliedTime
	}
	return nil
}

func Convert_v1beta2_ResourceBinding_To_v1alpha3_ResourceBinding(in *addonsv1.ResourceBinding, out *ResourceBinding, s apimachineryconversion.Scope) error {
	if err := autoConvert_v1beta2_ResourceBinding_To_v1alpha3_ResourceBinding(in, out, s); err != nil {
		return err
	}
	if !reflect.DeepEqual(in.LastAppliedTime, metav1.Time{}) {
		out.LastAppliedTime = ptr.To(in.LastAppliedTime)
	}
	return nil
}

// Implement local conversion func because conversion-gen is not aware of conversion func in other packages (see https://github.com/kubernetes/code-generator/issues/94)

func Convert_v1_Condition_To_v1alpha3_Condition(in *metav1.Condition, out *clusterv1alpha3.Condition, s apimachineryconversion.Scope) error {
	return clusterv1alpha3.Convert_v1_Condition_To_v1alpha3_Condition(in, out, s)
}

func Convert_v1alpha3_Condition_To_v1_Condition(in *clusterv1alpha3.Condition, out *metav1.Condition, s apimachineryconversion.Scope) error {
	return clusterv1alpha3.Convert_v1alpha3_Condition_To_v1_Condition(in, out, s)
}
